/**
  ******************************************************************************
  * @file    usart.c
  * @author  chengbb
  * @version V1.0
  * @date    2017-07-23
  * @brief   串口驱动
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2017 MindX</center></h2>
  ******************************************************************************
  */
/** @addtogroup BSP
* @{
*/
#include "stm32f10x.h"
#include "bsp_usart.h"
#include "ringbuf.h"
#include "wifi_config.h"
#include "log.h"

/** 串口接收回调 */
usart_recv_callback recv_cb1, recv_cb2, recv_cb3, recv_cb4, recv_cb5;



/**
  ******************************************************************************
  * @brief  串口初始化
  * @param  USARTx   串口号
  * @param  baudRate 波特率
  * @param  cb       串口接收中断回调
  * @retval None
  ******************************************************************************/
void Usart_Config(USART_TypeDef* USARTx, uint32_t baudRate, usart_recv_callback cb)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    if(USARTx == USART1)
    {
        recv_cb1 = cb;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

        /* Configure USART1 Tx (PA.09) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* Configure USART1 Rx (PA.10) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        USART_InitStructure.USART_BaudRate = baudRate;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        USART_Init(USART1, &USART_InitStructure);
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

        /* Enable the USARTy Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);

        USART_Cmd(USART1, ENABLE);
    }
    else if(USARTx == USART2)
    {
        recv_cb2 = cb;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

        /* Configure USART2 Tx (PA.02) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* Configure USART2 Rx (PA.03) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        USART_InitStructure.USART_BaudRate = baudRate;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        USART_Init(USART2, &USART_InitStructure);
        USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

        /* Enable the USARTy Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);

        USART_Cmd(USART2, ENABLE);
    }
    else if(USARTx == USART3)
    {
        recv_cb3 = cb;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

        /* Configure USART3 Tx (PB.10) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOB, &GPIO_InitStructure);

        /* Configure USART3 Rx (PB.11) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOB, &GPIO_InitStructure);

        USART_InitStructure.USART_BaudRate = baudRate;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        USART_Init(USART3, &USART_InitStructure);
        USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

        /* Enable the USARTy Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);

        USART_Cmd(USART3, ENABLE);
    }
    else if(USARTx == UART4)
    {
        recv_cb4 = cb;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);

        /* Configure UART4 Tx (PC.10) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOC, &GPIO_InitStructure);

        /* Configure UART4 Rx (PC.11) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_Init(GPIOC, &GPIO_InitStructure);

        USART_InitStructure.USART_BaudRate = baudRate;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        USART_Init(UART4, &USART_InitStructure);

        if(cb)
        {
            USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);

            /* Enable the USARTy Interrupt */
            NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
            NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
            NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
            NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
            NVIC_Init(&NVIC_InitStructure);
        }

        USART_Cmd(UART4, ENABLE);
        USART_ClearFlag(UART4,USART_FLAG_TC);
    }

    else if(USARTx == UART5)
    {
        recv_cb5 = cb;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);

        /* Configure UART5 Tx (PC.12) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOC, &GPIO_InitStructure);

        /* Configure UART5 Rx (PD.2) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOD, &GPIO_InitStructure);

        USART_InitStructure.USART_BaudRate = baudRate;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        USART_Init(UART5, &USART_InitStructure);

        if(cb)
        {
            USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);

            /* Enable the USARTy Interrupt */
            NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
            NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
            NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
            NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
            NVIC_Init(&NVIC_InitStructure);
        }

        USART_Cmd(UART5, ENABLE);
    }
}

/**
  ******************************************************************************
  * @brief  串口1中断函数
  * @retval None
  ******************************************************************************/
void USART1_IRQHandler(void)
{
    uint8_t ch;
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
        /* Read one byte from the receive data register */
        ch = USART_ReceiveData(USART1);
        recv_cb1(ch);
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    }
}

/**
  ******************************************************************************
  * @brief  串口2中断函数
  * @retval None
  ******************************************************************************/
void USART2_IRQHandler(void)
{
    uint8_t ch;
    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
    {
        /* Read one byte from the receive data register */
        ch = USART_ReceiveData(USART2);
        recv_cb2(ch);
        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
    }
}

/**
  ******************************************************************************
  * @brief  串口3中断函数
  * @retval None
  ******************************************************************************/
void USART3_IRQHandler(void)
{
    uint8_t ch;
    if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
    {
        /* Read one byte from the receive data register */
        ch = USART_ReceiveData(USART3);
        recv_cb3(ch);
        USART_ClearITPendingBit(USART3, USART_IT_RXNE);
    }
}

/**
  ******************************************************************************
  * @brief  串口4中断函数
  * @retval None
  ******************************************************************************/
void UART4_IRQHandler(void)
{
    uint8_t ch;
    if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
    {
        /* Read one byte from the receive data register */
        ch = USART_ReceiveData(UART4);
        recv_cb4(ch);
        USART_ClearITPendingBit(UART4, USART_IT_RXNE);
    }
}

/**
  ******************************************************************************
  * @brief  串口5中断函数
  * @retval None
  ******************************************************************************/
void UART5_IRQHandler(void)
{
    uint8_t ch;
    if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
    {
        /* Read one byte from the receive data register */
        ch = USART_ReceiveData(UART5);
        recv_cb5(ch);
        USART_ClearITPendingBit(UART5, USART_IT_RXNE);
    }
}

/**
  ******************************************************************************
  * @brief  发送字符串
  * @param  USARTx 	串口号
  * @param  *ch    	字符串指针
  * @param  len    	字符串长度
  * @retval 无
  ******************************************************************************
**/
void Usart_SendChar(USART_TypeDef* USARTx, char *ch, uint16_t len)
{
    uint16_t count = 0;
    for(; count < len; count++)
    {
        /* Write a character to the USART */
        USART_SendData(USARTx, *ch++);

        /* Loop until the end of transmission */
        while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
    }
}



/**
  ******************************************************************************
  * @brief  串口发送一个字符串
  * @param  USARTx 串口号
  * @param  fmt 格式
  * @retval 无
  ******************************************************************************/
void UsartPrintf(USART_TypeDef* USARTx, char* fmt,...)
{
    va_list ap;
    unsigned char UsartPrintfBuf[128];
    unsigned char *pStr = UsartPrintfBuf;

    va_start(ap, fmt);
    vsprintf((char *)UsartPrintfBuf, fmt, ap);							//格式化
    va_end(ap);

    while(*pStr != 0)
    {
        USARTx->DR = (*pStr++ & (uint16_t)0x01FF);
        while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);
    }

}

/**
  * @}
  */
/******************* (C) COPYRIGHT 2017 MINDX ******END OF FILE******************/
